package com.ruoyi.know.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.know.domain.UserComments;
import com.ruoyi.know.mapper.UserCommentsMapper;
import com.ruoyi.know.service.IUserCommentsService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 用户评论Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-03
 */
@Service
public class UserCommentsServiceImpl implements IUserCommentsService
{
    @Autowired
    private UserCommentsMapper userCommentsMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<UserComments> getCommentTree(Long infoId, Long commentType) {
        // 获取所有相关评论
        List<UserComments> allComments = userCommentsMapper.selectByInfoIdAndType(infoId, commentType);

        // 构建评论树
        Map<Long, UserComments> commentMap = new HashMap<>();
        List<UserComments> rootComments = new ArrayList<>();

        // 第一次遍历：初始化所有节点并找到根评论
        for (UserComments comment : allComments) {
            Long userId = comment.getUserId();
            SysUser sysUser = sysUserMapper.selectUserById(userId);
            comment.setAuthor(sysUser);
            comment.setChildren(new ArrayList<>()); // 初始化子列表
            commentMap.put(comment.getCommentId(), comment);

            if (comment.getpCommentId() == null || comment.getpCommentId() == 0) {
                rootComments.add(comment);
            }
        }

        // 第二次遍历：建立父子关系
        for (UserComments comment : allComments) {
            Long userId = comment.getUserId();
            SysUser sysUser = sysUserMapper.selectUserById(userId);
            comment.setAuthor(sysUser);
            if (comment.getpCommentId() != null && comment.getpCommentId() != 0) {
                UserComments parent = commentMap.get(comment.getpCommentId());
                if (parent != null) {
                    parent.getChildren().add(comment);
                }
            }
        }

        return rootComments;
    }

    /**
     * 查询用户评论
     *
     * @param commentId 用户评论主键
     * @return 用户评论
     */
    @Override
    public UserComments selectUserCommentsByCommentId(Long commentId)
    {
        return userCommentsMapper.selectUserCommentsByCommentId(commentId);
    }

    /**
     * 查询用户评论列表
     *
     * @param userComments 用户评论
     * @return 用户评论
     */
    @Override
    public List<UserComments> selectUserCommentsList(UserComments userComments)
    {
        List<UserComments> comments = userCommentsMapper.selectUserCommentsList(userComments);
        for (UserComments comment : comments){
            Long userId = comment.getUserId();
            SysUser sysUser = sysUserMapper.selectUserById(userId);
            comment.setAuthor(sysUser);
        }

        return comments;
    }

    /**
     * 新增用户评论
     *
     * @param userComments 用户评论
     * @return 结果
     */
    @Override
    public int insertUserComments(UserComments userComments)
    {
        return userCommentsMapper.insertUserComments(userComments);
    }

    /**
     * 修改用户评论
     *
     * @param userComments 用户评论
     * @return 结果
     */
    @Override
    public int updateUserComments(UserComments userComments)
    {
        return userCommentsMapper.updateUserComments(userComments);
    }

    /**
     * 批量删除用户评论
     *
     * @param commentIds 需要删除的用户评论主键
     * @return 结果
     */
    @Override
    public int deleteUserCommentsByCommentIds(Long[] commentIds)
    {
        return userCommentsMapper.deleteUserCommentsByCommentIds(commentIds);
    }

    /**
     * 删除用户评论信息
     *
     * @param commentId 用户评论主键
     * @return 结果
     */
    @Override
    public boolean deleteUserCommentsByCommentId(Long commentId)
    {
        // 检查是否有子评论
        int childCount = userCommentsMapper.countChildrenByPCommentId(commentId);
        if (childCount > 0) {
            return false; // 有子评论，不能删除
        }
        // 没有子评论，执行删除操作
        return userCommentsMapper.deleteUserCommentsByCommentId(commentId)>0;
    }

    @Override
    public Integer getCountByComments(UserComments userComments) {
        return userCommentsMapper.getCommentsCount(userComments);
    }


}
